// app.project.removeUnusedFootage();
app.project.consolidateFootage();
organizeProjectAssets();
function organizeProjectAssets() {
    try {
        var opaDefaults = new Object();
        opaDefaults.myFolders = new Array("Comps", "Footage", "tmp", "Solids", " Precomps");
        opaDefaults.myFolderObjects = new Array();
        opaDefaults.myPlaceholderObjects = new Array();
        if (app.project.file != null) {
            opaProcess();
        } else {
            alert("Please open a project or save the current project first.");
        }

        function retrieveProjectItems(itemType) {
            var typeOptions = new Array("Composition", "Folder", "Footage");
            for (var t = 0; t < 3; t += 1) {
                if (itemType == typeOptions[t]) {
                    itemAry = new Array();
                    proj = app.project;
                    itemTotal = proj.numItems;
                    for (var i = 1; i <= itemTotal; i += 1) {
                        curItem = proj.item(i);
                        if (curItem.typeName == itemType) {
                            itemAry[itemAry.length] = curItem;
                        }
                    }
                    return itemAry;
                }
            }
        }

        function moveToFolder(curItemAry, parFolder) {
            var itemAryLength = curItemAry.length;
            for (var i = 0; i < itemAryLength; i += 1) {
                curItemAry[i].parentFolder = parFolder;
            }
        }

        function grabPlaceholders() {
            var ni = app.project.numItems;
            for (var n = 1; n <= ni; n += 1) {
                curItem = app.project.item(n);
                for (var i = 0; i < 4; i += 1) {
                    if (curItem instanceof FootageItem && curItem.name == "Placeholder") {
                        opaDefaults.myPlaceholderObjects[opaDefaults.myPlaceholderObjects.length] = curItem;
                        break;
                    }
                }
            }
            return opaDefaults.myPlaceholderObjects;
        }

        function grabMyFolderObjects() {
            var ni = app.project.numItems;
            for (var n = 1; n <= ni; n += 1) {
                curItem = app.project.item(n);
                for (var i = 0; i < 4; i += 1) {
                    if (curItem instanceof FolderItem && curItem.name == opaDefaults.myFolders[i]) {
                        opaDefaults.myFoldersObjects[opaDefaults.myFoldersObjects.length] = curItem;
                        break;
                    }
                }
            }
        }

        function grabSpecificExtType(itemType, extName, stillVideo) {
            tmp = new Array();
            proj = app.project;
            pi = proj.numItems;
            for (var i = 1; i <= pi; i += 1) {
                curItem = proj.item(i);
                if (eval("curItem instanceof " + itemType)) {
                    ext = grabExt(curItem.name);
                    userExt = extName.toString().toLowerCase();
                    if (ext == userExt) {
                        if (curItem.duration > 1 && stillVideo == "Video") {
                            tmp[tmp.length] = curItem;
                        } else if (curItem.duration == 0 && stillVideo == "Still") {
                            tmp[tmp.length] = curItem;
                        } else {
                            tmp[tmp.length] = curItem;
                        }
                    }
                }
            }
            if (tmp.length >= 1) {
                return tmp;
            } else {
                return null;
            }
        }

        function grabAllExt() {
            tmp = new Array();
            proj = app.project;
            ni = proj.numItems;
            ph = false;
            for (var i = 1; i <= ni; i += 1) {
                curItem = proj.item(i);
                var n = curItem.name.split(".");
                if (curItem instanceof FootageItem) {
                    if (n.length > 1) {
                        tmp[tmp.length] = n[n.length - 1].toString().toLowerCase();
                    }
                }
            }
            tmp = sortAndRemoveDups(tmp);
            return tmp;
        }

        function sortAndRemoveDups(aryInput) {
            var arySorted = aryInput.sort();
            var aryLength = arySorted.length;
            var results = new Array();
            for (var i = 0; i < aryLength; i += 1) {
                if (arySorted[i] != arySorted[i + 1]) {
                    results[results.length] = arySorted[i];
                }
            }
            return results;
        }

        function grabAllSolids() {
            itemAry = new Array();
            proj = app.project;
            itemTotal = proj.numItems;
            for (var i = 1; i <= itemTotal; i += 1) {
                curItem = proj.item(i);
                if (curItem.duration == 0) {
                    if (curItem.mainSource.color) {
                        itemAry[itemAry.length] = curItem;
                    }
                }
            }
            return itemAry;
        }

        function grabAllPrecomps() {
            itemAry = new Array();
            proj = app.project;
            itemTotal = proj.numItems;
            for (var i = 1; i <= itemTotal; i += 1) {
                curItem = proj.item(i);
                if (curItem instanceof CompItem) {
                    if (curItem.usedIn.length > 0) {
                        itemAry[itemAry.length] = curItem;
                    }
                }
            }
            return itemAry;
        }

        function opaProcess() {
            projExt = grabAllExt().sort();
            projExtLength = projExt.length;
            var projectComps = retrieveProjectItems("Composition");
            var projectCompsLength = projectComps.length;
            var projectFootage = retrieveProjectItems("Footage");
            var projectFootageLength = projectFootage.length;
            var projectFolder = retrieveProjectItems("Folder");
            var projectFolderLength = projectFolder.length;
            var solids = grabAllSolids();
            var solidsLength = solids.length;
            var preComps = grabAllPrecomps();
            var preCompsLength = preComps.length;
            var placeHolders = grabPlaceholders();
            var placeHoldersLength = placeHolders.length;
            cFolder = app.project.items.addFolder(opaDefaults.myFolders[0]);
            fFolder = app.project.items.addFolder(opaDefaults.myFolders[1]);
            tmpFolder = app.project.items.addFolder(opaDefaults.myFolders[2]);
            sFolder = app.project.items.addFolder(opaDefaults.myFolders[3]);
            pcFolder = app.project.items.addFolder(opaDefaults.myFolders[4]);
            if (placeHoldersLength > 0) {
                phFolder = app.project.items.addFolder("Placeholder");
                phFolder.parentFolder = fFolder;
            }
            moveToFolder(projectFolder, tmpFolder);
            moveToFolder(projectComps, cFolder);
            moveToFolder(preComps, pcFolder);
            pcFolder.parentFolder = cFolder;
            moveToFolder(projectFootage, fFolder);
            for (var pe = 0; pe < projExtLength; pe += 1) {
                pf = app.project.items.addFolder(projExt[pe]);
                grabItems = grabSpecificExtType("FootageItem", projExt[pe].toString(), "");
                moveToFolder(grabItems, pf);
                pf.parentFolder = fFolder;
            }
            if (placeHoldersLength > 0) {
                moveToFolder(placeHolders, phFolder);
            }
            moveToFolder(solids, sFolder);
            tmpFolder.remove();
        }
    } catch (err) {
        alert("ERROR:\nSomething went wrong, please email torno@sydefxink.com with the info below:\n--------------------------------------\nOS: " + $.os + " (" + $.locale + ")\nAE: " + app.version + " (" + app.language + ")\n\nError at line #" + err.line + "\n\n" + err.toString());
    }
}